﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
namespace DAO
{
    public class AccountDAO
    {
        private static DataClasses1DataContext m_nhomMuaDataContext = new DataClasses1DataContext();
        public static bool insertAccount(ACCOUNT mAccount)
        {
            try
            {
                m_nhomMuaDataContext.ACCOUNTs.InsertOnSubmit(mAccount);
                m_nhomMuaDataContext.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool updateAccount(ACCOUNT mAccount)
        {
            try
            {
                ACCOUNT account = m_nhomMuaDataContext.ACCOUNTs.Single(temp => temp.USERNAME == mAccount.USERNAME);
                account.ADDRESS = mAccount.ADDRESS;
                account.CMND = mAccount.CMND;
                account.FULLNAME = mAccount.FULLNAME;
                account.PASSWORD = mAccount.PASSWORD;
                account.PHONE_NUMBER = mAccount.PHONE_NUMBER;
                m_nhomMuaDataContext.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static ACCOUNT getAccount(string user_login)
        {
            try
            {
                ACCOUNT account= m_nhomMuaDataContext.ACCOUNTs.Single(temp => temp.USERNAME == user_login);
                return account;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static ACCOUNT getAccountFromEmail(string user_email)
        {
            try
            {
                ACCOUNT account = m_nhomMuaDataContext.ACCOUNTs.Single(temp => temp.EMAIL == user_email);
                return account;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static bool DeleteAccount(int IDAccount)
        {
            try
            {
                ACCOUNT account = m_nhomMuaDataContext.ACCOUNTs.Single(temp => temp.IDACCOUNT == IDAccount);
                if (account != null)
                {
                    m_nhomMuaDataContext.ACCOUNTs.DeleteOnSubmit(account);
                    m_nhomMuaDataContext.SubmitChanges();
                    return true;
                }

                return false;
            }
            catch
            {
                return false;
            }

        }
        public static ACCOUNT GetAccount(string UserName, string PassWord)
        {
            try
            {
                ACCOUNT account = m_nhomMuaDataContext.ACCOUNTs.Single(mem => (mem.USERNAME.Trim() == UserName) && (mem.PASSWORD.Trim() == PassWord));
                return account;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public static int GetIDMax()
        {
            int maxID = m_nhomMuaDataContext.ACCOUNTs.Max(comment => comment.IDACCOUNT);
            return maxID;
        }

        public static bool IsExisting(string UserName, string PassWord)
        {
            return m_nhomMuaDataContext.ACCOUNTs.Any(mem => (mem.USERNAME.Trim() == UserName) && (mem.PASSWORD.Trim() == PassWord));
        }

        public static bool IsExisting(string UserName)
        {
            return m_nhomMuaDataContext.ACCOUNTs.Any(mem => mem.USERNAME.Trim() == UserName);
        }

        public static bool IsExisting(int IDAccount)
        {
            return m_nhomMuaDataContext.ACCOUNTs.Any(mem => mem.IDACCOUNT == IDAccount);
        }

        public static bool IsExistingEmail(string email)
        {
            return m_nhomMuaDataContext.ACCOUNTs.Any(mem => mem.EMAIL == email);
        }

        public static bool CheckIsExisting(string userName)
        {
            return m_nhomMuaDataContext.ACCOUNTs.Any(acc => acc.USERNAME.Trim() == userName.Trim());
        }
       
    }
}
